package com.example.bishe.controller.dw;

import com.alibaba.fastjson.JSON;
import com.example.bishe.entity.Jxbm;
import com.example.bishe.entity.Zxbz_dw;
import com.example.bishe.service.JxbmService;
import com.example.bishe.service.Zxbz_dwService;
import com.example.bishe.service.impl.JxbmServiceImpl;
import com.example.bishe.service.impl.Zxbz_dwServiceImpl;
import com.example.bishe.toolclass.CUD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yws
 * @Date 2022/4/4 16:23
 */
@RestController
@RequestMapping("/dw")
public class DwController {
    @Resource
    CUD CUD;
    @Autowired
    Zxbz_dwService dw = new Zxbz_dwServiceImpl();
    @Autowired
    JxbmService jxbmService = new JxbmServiceImpl();
    @PostMapping("/adddw")
    public String adddw(@RequestBody Zxbz_dw zxbz_dw) {
        int errcode = 0;//错误代码
        String errmsg =" ";//错误描述
        int id;//返回创建成功的id
        HashMap value = null;
        Zxbz_dw zxbzDw = new Zxbz_dw();zxbzDw.setDm(zxbz_dw.getDm());
        Zxbz_dw[] zxbz_dws = dw.select_dw(zxbzDw);
        Jxbm jxbm = new Jxbm();jxbm.setDm(zxbz_dw.getLs());
        Jxbm[] jxbms = jxbmService.selectJxbm(jxbm);
        if(zxbz_dw.getDm()==null)
            errmsg+="请求中没有添加单位代码，";
        if(zxbz_dw.getMc()==null)
            errmsg+="请求中没有添加单位名称，";
        if(zxbz_dw.getLs()==null)
            errmsg+="请求中没有添加单位隶属，";
        if(zxbz_dw.getWx_parentid()!=0){
            errmsg+="请求中不能添加父部门id字段，请设置隶属部门字段，";
        }
        if(errmsg.length()>1){
            errcode = 1301;
            errmsg = errmsg.substring(1, errmsg.length() - 1) + "。";
        }else if(zxbz_dws.length==0){
            if(jxbms.length!=0){
                jxbm = jxbms[0];
                value = CUD.add(zxbz_dw.getMc(),
                        jxbm.getWx_id()+"",
                        zxbz_dw.getWx_id()!=0?zxbz_dw.getWx_id()+"":"");
                if (value.get("errcode").equals("0")) {
                    errcode = 0;
                    //可以插入数据库操作
                    zxbz_dw.setWx_id(Integer.parseInt((String) value.get("id")));
                    zxbz_dw.setWx_parentid(jxbm.getWx_id());
                    dw.insert_dw(zxbz_dw);
                    errmsg += "无错误。";
                    errmsg = errmsg.substring(1, errmsg.length());
                }else {
                    errcode = 1301;
                    errmsg+= CUD.addpan(value);
                }
            }else {
                errcode = 1301;
                errmsg += "你所要添加的隶属单位不存在。";
            }
        }else {
            errcode = 1301;
            errmsg += "你所创建的单位代码已经存在。";
            errmsg = errmsg.substring(1, errmsg.length());
        }
        Map<String, String> map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        if (zxbz_dw.getMc() == null) {
            map.put("mingcheng", "null");
        } else {
            map.put("mingcheng", zxbz_dw.getMc());
        }
        String s = JSON.toJSONString(map);
        return s;
    }
    @PostMapping("/deletedw")
    public String deletedw(@RequestBody Zxbz_dw zxbz_dw) {
        int errcode = 0;//错误代码
        String errmsg = "";//错误描述
        if (zxbz_dw.getDm() == null) {
            errcode = 1402;
            errmsg = "请传入你要删除的单位的代码";
        }else {
            Zxbz_dw zxbzDw = new Zxbz_dw();zxbzDw.setDm(zxbz_dw.getDm());
            Zxbz_dw[] zxbz_dws = dw.select_dw(zxbzDw);
            if(zxbz_dws.length!=0){
                zxbzDw = zxbz_dws[0];
                HashMap<String, String> value = CUD.delete(zxbzDw.getWx_id()+"");
                if (value.get("errcode").equals("0")) {
                    dw.delete_dw(zxbzDw);
                    errmsg = "已删除对应的教职工单位";
                } else {
                    errmsg+= CUD.addstudent_jzgpan(value);
                    errcode=1402;
                }
            }else {
                errcode=1001;
                errmsg = "没有找到你要删除的单位代码";
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }
    @PostMapping("/updatedw")
    public String updatedw(@RequestBody Zxbz_dw zxbz_dw) {
        int errcode = 0;//错误代码
        String errmsg = "";//错误描述
        if(zxbz_dw.getWx_id()!=0||zxbz_dw.getWx_parentid()!=0){
            errcode = 1303;
            errmsg = "不能修改单位的微信id，父部门id请修改部门的隶属教学部门代码";
        }
        else if (zxbz_dw.getDm() == null) {
            errcode = 1303;
            errmsg = "请传入你要修改的教职工单位代码";
        }else {
            Zxbz_dw zxbzDw = new Zxbz_dw();zxbzDw.setDm(zxbz_dw.getDm());
            Zxbz_dw[] zxbz_dws = dw.select_dw(zxbzDw);
            if(zxbz_dws.length!=0){/*存在对应的单位代码*/
                zxbzDw = zxbz_dws[0];
                if(zxbz_dw.getLs()!=null){/*如果需要修改部门隶属*/
                    Jxbm jxbm = new Jxbm();jxbm.setDm(zxbz_dw.getLs());
                    Jxbm[] jxbms = jxbmService.selectJxbm(jxbm);
                    if(jxbms.length!=0){
                        jxbm = jxbms[0];
                        HashMap<String, String> value = CUD.update(zxbzDw.getWx_id()+"",
                                zxbz_dw.getMc()!=null?zxbz_dw.getMc():zxbzDw.getMc(),
                                jxbm.getWx_id()+"");
                        System.out.println(value.get("errcode"));System.out.println(value.get("errmsg"));
                        if(value.get("errcode").equals("0")){
                            errmsg+="已修改对应信息";
                            zxbz_dw.setWx_parentid(jxbm.getWx_id());
                            zxbz_dw.setWx_id(zxbzDw.getWx_id());
                            dw.query_dw(zxbz_dw);
                        }else {
                            errcode=1303;
                            errmsg+= CUD.updatepan(value);
                        }
                    }else {
                        errcode=1303;
                        errmsg+= "没有找到对应的上层隶属单位";
                    }
                }else {/*不需要修改部门的隶属*/
                    HashMap<String, String> value = CUD.update(zxbzDw.getWx_id()+"",
                            zxbz_dw.getMc()!=null?zxbz_dw.getMc():zxbzDw.getMc(),
                            zxbzDw.getWx_parentid()+"");
                    if(value.get("errcode").equals("0")){
                        errmsg+="已修改对应信息";
                        zxbz_dw.setWx_parentid(zxbzDw.getWx_parentid());
                        zxbz_dw.setWx_id(zxbzDw.getWx_id());
                        dw.query_dw(zxbz_dw);
                    }else {
                        errcode=1303;
                        errmsg+= CUD.updatepan(value);
                    }
                }
            }else {
                errcode=1303;
                errmsg+= "没有找到对应的单位代码";
            }

        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }
    @PostMapping("/selectdw")
    public String selectdw(@RequestBody Zxbz_dw zxbz_dw) {
        Zxbz_dw[] zxbz_dws = dw.select_dw(zxbz_dw);
        List<Zxbz_dw> list = new ArrayList<>();
        for (int i = 0; i < zxbz_dws.length; i++) {
            list.add(zxbz_dws[i]);
        }
        Map map = new HashMap();
        map.put("Zxbz_dw",list);
        String s = JSON.toJSONString(map);
        return s;
    }
}
